from src.handler.talent import TalentHandler
from src.config.talents import *

class Talent:
    
    def __init__(self, name, entity):
        self.name = name
        self.entity = entity
        self.stamina = 0 # health
        self.intelligence = 0 # mana
        self.strength = 0 # attack
        self.agility = 0 # attack
        self.armor = 0
        self.active = 0
        self.abilities = []
        
    def addHealth(self):
        #self.entity.maxHealth += self.stamina * 10
        #self.entity.health = self.entity.maxHealth
        pass
    
    def add(self, index, ability):
        self.abilities.insert(0, ability)
    
    def switchAbility(self, count):
        self.active += count
        if self.active >= len(self.abilities):
            self.active = 0
        if self.active < 0:
            self.active = len(self.abilities) - 1
    
    def cast(self):
        if self.abilities[self.active] is not None:
            self.abilities[self.active].cast(self.entity.world.parent)
    
    """
    Handles incoming damage for an EntityLiving
    source: 0 = Block, 1 = Entity
    name: Block/Entity name
    damage: amount of damage in health
    health: current health
    return: health after damage
    """
    def handleDamage(self, source, name, damage, health):
        return health - max(damage - self.armor, 0)
    
    def readFromPSO(self, pso):
        self.name = pso.get("name")
        self.stamina = pso.get("stamina")
        self.intelligence = pso.get("intelligence")
        self.strength = pso.get("strength")
        self.agility = pso.get("agility")
        self.armor = pso.get("armor")
        self.active = pso.get("active")
        if self.active is None:
            self.active = 0
        
    @staticmethod
    def loadFromPSO(pso):
        name = pso.get("name")
        talent = TalentHandler.get(name, None)
        talent.readFromPSO(pso)
        return talent
    
    def writeToPSO(self, pso):
        pso.set("name", self.name)
        pso.set("stamina", self.stamina)
        pso.set("intelligence", self.intelligence)
        pso.set("strength", self.strength)
        pso.set("agility", self.agility)
        pso.set("armor", self.armor)
        pso.set("active", self.active)
        